package com.xmler.plugin.adbrocket.adb.command

import com.android.ddmlib.IDevice
import com.android.ddmlib.MultiLineReceiver
import com.intellij.openapi.project.Project
import com.xmler.plugin.adbrocket.utils.Logger
import org.jetbrains.android.facet.AndroidFacet
import java.util.concurrent.TimeUnit
import javax.swing.JTextArea

class InputAnyCmd(val cmd: String, val receiver: MultiLineReceiver): ICommand {
    override fun run(
        project: Project,
        device: IDevice,
        facet: AndroidFacet,
        packageName: String,
        display: JTextArea
    ): Boolean {
        try {
            device.executeShellCommand(cmd, WrapperReceiver(receiver, display), 10L, TimeUnit.SECONDS)
            return true
        } catch (e: Exception) {

        }
        return false
    }
}

internal class WrapperReceiver(val receiver: MultiLineReceiver, val display: JTextArea): MultiLineReceiver() {
    override fun isCancelled(): Boolean = receiver.isCancelled

    override fun processNewLines(lines: Array<out String>) {
        for (line in lines) {
            Logger.d(line)
        }
        receiver.processNewLines(lines)
    }

}